<template>
	<view class="backList">
		<!-- 材料归还 待归还 材料归还 -->
		<navBar title="材料归还" goBackUrl="/subpackage-materials/pages/materials/materialsGoBack/materialsGoBack" @goBack="goBack"></navBar>
		
		<scroll-view scroll-y="true" class="scroll-box">
			<view class="content">
				<view class="project">
					<image class="image" src="/subpackage-materials/static/project1.png" mode=""></image>
					<view class="name">
						<view class="t1">{{info.orgName}}</view>
						<view class="t2">{{info.siteName}}</view>
					</view>
				</view>
				
				<view class="box uniCollapse">
					<view class="nav border">
						<view class="name">出库编号</view>
						<view class="value">{{info.code}}</view>
					</view>
					
					<view class="nav border">
						<view class="name">材料出库单归还</view>
						<view class="value width">
							<checkbox-group v-show="goBackAllShow" @change="allCheckboxChange" class="radio">
								<label class="label">
									<text class="text">{{ materialAllObj.name }}</text>
									<checkbox :value="materialAllObj.value"  :checked="materialAllObj.check" class="color round"/>
								</label>
							</checkbox-group>
						</view>
					</view>
			
					<uni-collapse>
					    <uni-collapse-item v-for="(item,index) in goBackList" title-border="show" :key="index" :title="item.name" :value="item.list.length" >
							<template v-slot:title>
								<view class="collapse-title">
									<view class="left">
										{{ item.name }}
									</view>
									<view class="right">
										{{ item.list.length }}
									</view>
								</view>
							</template>
							<checkbox-group @change="curr=>checkboxChange(curr,index)">
								<label v-for="(i,j) in item.list" :key='j'>
									<view class="center">
										<view class="item">
											<view class="name">规格型号：</view>
											<view class="value">{{i.specificationModel}}</view>
										</view>
										<view class="item">
											<view class="name">过保日期：</view>
											<view class="value">{{i.time}}</view>
										</view>
									<!-- 	<view class="item">
											<view class="name">存储区域：</view>
											<view class="value">{{i.storageAreaName}}</view>
										</view> -->
										<view class="item">
											<view class="name">供应商：</view>
											<view class="value">{{i.supplier}}</view>
										</view>
										<view class="item">
											<view class="name">出库数量：</view>
											<view class="value">{{i.exNum}}吨</view>
										</view>
										<view class="item m40">
											<view class="name">归还数量：</view>
											<view class="value right">
												<u-number-box class="number" :min="0" :max="i.exNum" v-model="i.outNum" @change="curr=>comeNumChange(curr,j,index)" color="#323233"></u-number-box>
												<text class="unit">吨</text>
											</view>
										</view>
										<checkbox :value="String(i.tempCode)" :checked="i.check" class="checkbox color round" />
									</view>
								</label>
							</checkbox-group>
					    </uni-collapse-item>
					  </uni-collapse>
				</view>
				
				<!-- 临时材料归还 -->
				<view class="box2 uniCollapse">
					<view class="nav bgc border">
						<view class="name">临时材料归还</view>
						<view class="value width">
							<checkbox-group v-show="tmpAllShow" @change="tmpAllCheckboxChange" class="radio">
								<label class="label">
									<text class="text">{{ tempMaterialAllObj.name }}</text>
									<checkbox :value="tempMaterialAllObj.value" :checked="tempMaterialAllObj.check" class="color round"/>
								</label>
							</checkbox-group>
						</view>
					</view>
					
					<uni-collapse>
					    <uni-collapse-item v-for="(item,index) in tmpList" title-border="show" :key="index" :title="item.name" :value="item.list.length" >
							<template v-slot:title>
								<view class="collapse-title">
									<view class="left">
										{{ item.name }}
									</view>
									<view class="right">
										{{ item.list.length }}
									</view>
								</view>
							</template>
							<checkbox-group @change="curr=>tmpCheckboxChange(curr,index)">
								<label v-for="(i,j) in item.list" :key="j">
									<view class="center">
										<view class="item">
											<view class="name">规格型号：</view>
											<view class="value">{{i.specificationModel}}</view>
										</view>
										<view class="item">
											<view class="name">生产日期：</view>
											<view class="value">{{i.productionDatetime}}</view>
										</view>
								<!-- 		<view class="item">
											<view class="name">存储区域：</view>
											<view class="value">{{i.storageAreaName}}</view>
										</view> -->
										<view class="item">
											<view class="name">供应商：</view>
											<view class="value">{{i.supplier}}</view>
										</view>
										<view class="item">
											<view class="name">保质期：</view>
											<view class="value">{{i.shelfLife}}</view>
										</view>
										<view class="item">
											<view class="name">出库数量：</view>
											<view class="value">{{i.exNum}}{{i.unit}}</view>
										</view>
							<!-- 			<view class="item">
											<view class="name">采购单价：</view>
											<view class="value">{{i.price}}元</view>
										</view> -->
										<view class="item m40">
											<view class="name">归还数量：</view>
											<view class="value right">
												<u-number-box class="number" :min='0' :max='i.exNum' v-model="i.outNum" @change="curr=>tmpComeNumChange(curr,j,index)" color="#323233"></u-number-box>
												<text class="unit">吨</text>
											</view>
										</view>
										<checkbox :value="String(i.tempCode)" :checked="i.check" class="checkbox color round" />
									</view>
								</label>
							</checkbox-group>
					    </uni-collapse-item>
					</uni-collapse>
				</view>
				<view class="fixedButton">
					<u-button type="primary"  :loading="loading" text="提交" @click="submit"></u-button>
				</view>
			</view>
		</scroll-view>
		
	</view>
</template>

<script>
	import { getMaterialOutCode, getMaterialBackSaveEx } from "@/api/materials/materials.js"
	import { mapMutations, mapState } from "vuex"
 	import mixins from "@/utils/mixins.js"
	import uniCollapse from "./../../../components/uni-collapse/components/uni-collapse/uni-collapse.vue";
	
	export default {
		name: "backList",
 		mixins: [mixins],
		components: {
			uniCollapse
		},
		data() {
			return {
				list: [
				],
				info:{},
				comeNum: "",
				goBackList:[],
				tmpList:[],
				goBackAllShow:false,
				tmpAllShow:false,
				loading:false,
				materialAllObj:{      // 材料全选
					name : '全选',
					value : 'all',
					check : false,	
				},
				tempMaterialAllObj:{      // 临时材料全选
					name : '全选',
					value : 'all',
					check : false,	
				},
			}
		},		
		computed: {
			...mapState('materials',['materialsGoBackSubmitItem'])
		},
		mounted() {
			this.init()
		},
		methods: {
			...mapMutations('materials', ["setGoBackSubmitList"]),
			init(){
				this.info=this.materialsGoBackSubmitItem
				this.info.siteName=this.info.projectCascade.split('/')[1]+"/"+this.info.projectCascade.split('/')[2]
				this.getList()
			},
			getList(){
				getMaterialOutCode({code:this.info.code}).then(res=>{
					if(res.success){
						res.result.forEach((item,index)=>{
							item.tempCode = index
						})
						let list=res.result.filter(item=>item.type===0)
						//获取所有的类型
						let typeList=Array.from(new Set(list.map(item=>item.name)))
						typeList.forEach(item=>{
							let itemObj={
								name:item,
								list:[],
								checkAll:false //是否全选
							}
							list.forEach(i=>{
								if(item===i.name){
									i.check=false  //每个字段的选择框
									i.outNum=0
									if(i.expirationDate){
										i.time=this.getDate(i.expirationDate)
									}
									itemObj.list.push(i)
								}
							})
							this.goBackList.push(itemObj)
						})
						this.goBackAllShow=typeList.length>0
						//临时信息
						let tmpList=res.result.filter(item=>item.type===1)
						let tmpTypeList=Array.from(new Set(tmpList.map(item=>item.name)))
						tmpTypeList.forEach(item=>{
							let itemObj={
								name:item,
								list:[],
								checkAll:false //是否全选
							}
							tmpList.forEach(i=>{
								if(item===i.name){
									i.check=false  //每个字段的选择框
									i.outNum=0
									if(i.expirationDate){
										i.time=this.getDate(i.expirationDate)
									}
									if(i.productionDate){
										i.productionDatetime=this.getDate2(i.productionDate)
									}
									itemObj.list.push(i)
								}
							})
							this.tmpList.push(itemObj)
						})
						this.tmpAllShow=tmpTypeList.length
						console.log(this.tmpList)
					}
				})
			},
			
			// 计数器change事件
			comeNumChange(curr,i,index){
				console.log(curr,i, index)
				this.goBackList[index].list[i].outNum=curr
				console.log(this.goBackList,'1111111111111')
			},
			
			// 材料选中状态
			checkboxChange(curr,index){
				let arr=curr.detail.value
				console.log(arr,'arr')
				this.goBackList[index].list.forEach(item=>{
					if(arr.includes(item.tempCode + "")){
						console.log('000000')
						this.$set(item, "check", true)
					}else{
						console.log('111111')
						this.$set(item, "check", false)
					}
				})
				
				let _index = 0
				let list = []
				this.goBackList.forEach(item=>{
					item.list.forEach(value=>{
						list.push(value)
					})
				})
				
				
				list.forEach(item=>{
					if(item.check){
						_index ++ 
					}
				})
				console.log(list.length,_index,'list')
				if(_index === list.length){
					this.$set(this.materialAllObj, "check", true)
				}else{
					this.$set(this.materialAllObj, "check", false)
				}
			},
			
			// 材料全选
			allCheckboxChange(val){
				let content = val.detail.value
				if(content && content.length > 0){
					this.goBackList.forEach(item=>{
						item.list.forEach(i=>{
							i.check=true
						})
					})
				}else{
					this.goBackList.forEach(item=>{
						item.list.forEach(i=>{
							i.check=false
						})
					})
				}
				
			},
			
			
			// 临时材料选中
			tmpCheckboxChange(curr,index){
				let arr=curr.detail.value
				console.log(arr,'arr')
				this.tmpList[index].list.forEach(item=>{
					if(arr.includes(item.tempCode + "")){
						this.$set(item, "check", true)
					}else{
						this.$set(item, "check", false)
					}
				})
				
				let _index = 0
				let list = []
				this.tmpList.forEach(item=>{
					item.list.forEach(value=>{
						list.push(value)
					})
				})
				
				
				list.forEach(item=>{
					if(item.check){
						_index ++ 
					}
				})
				if(_index === list.length){
					this.$set(this.tempMaterialAllObj, "check", true)
				}else{
					this.$set(this.tempMaterialAllObj, "check", false)
				}
			},
			
			// 临时材料归还全选
			tmpAllCheckboxChange(e){
				let content = e.detail.value
				if(content && content.length > 0){
					this.tmpList.forEach(item=>{
						item.list.forEach(i=>{
							i.check=true
						})
					})
				}else{
					this.tmpList.forEach(item=>{
						item.list.forEach(i=>{
							i.check=false
						})
					})
				}
			},
			tmpComeNumChange(curr,i,index){
				this.tmpList[index].list[i].outNum=curr
				console.log(curr)
				console.log(i)
				console.log(index)
				console.log(this.tmpList)
			},
			submit(){
				let res=[]
				this.goBackList.forEach(item=>{
					item.list.forEach(i=>{
						if(i.check && i.outNum>0){
							res.push(i)
						}
					})
				})
				this.tmpList.forEach(item=>{
					item.list.forEach(i=>{
						if(i.check && i.outNum>0){
							res.push(i)
						}
					})
				})
				console.log(res,'res',this.goBackList,this.tmpList)
				if(res.length===0){
					uni.showToast({
						title:'请先选择材料',
						icon:'error'
					})
					return
				}
				this.setGoBackSubmitList(res)
				
				console.log('跳转')
				uni.navigateTo({
				     url: "/subpackage-materials/pages/materials/materialsGoBack/components/backConfirm"
				})
			},
			
			goBack(url){
				uni.navigateBack({
					url : url
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.backList{
		padding-bottom: 120rpx;
		/* #ifdef MP-WEIXIN */
			padding-top: 180rpx;
		/* #endif */
		font-family: PingFang SC-Bold, PingFang SC;
		.scroll-box{
			max-height: calc(100vh - 170rpx);
			padding-top: 10rpx;
		}
		.content{
			background: #F5F7FA;
			padding-top: 30rpx;
			.project{
				background-color: #FFFFFF;
				padding: 0rpx 30rpx;
				height: 150rpx;
				display: flex;
				align-items: center;
				padding-top: 40rpx;
				box-sizing: border-box;
				.image{
					width: 70rpx;
					height: 70rpx;
					margin-left: 29rpx;
					margin-right: 40rpx;
				}
				.name{
					.t1{
						font-size: 32rpx;
						font-weight: bold;
						color: #445469;
					}
					.t2{
						font-size: 28rpx;
						font-weight: 500;
						color: #6A819E;
						margin-top: 5rpx;
					}
				}
			}
			
			.box{
				padding: 0rpx 30rpx;
				background-color: #FFFFFF;
			}
			.box2{
				padding: 0rpx 30rpx;
				background-color: #FFFFFF;
			}
			.nav{
				line-height: 100rpx;
				height: 100rpx;
				padding: 0rpx 28rpx;
				display: flex;
				justify-content: space-between;
				
				.name{
					font-size: 32rpx;
					font-weight: bold;
					color: #445469;
				}
				.value{
					font-size: 32rpx;
					font-weight: bold;
					color: #445469;
				}
				.width{
					width: 150rpx;
					.label{
						display: flex;
						align-items: center;
						justify-content: space-between;
					}
				}
			}
			.border{
				border-bottom: 1rpx solid #d6d7d9;
			}
			
			
			.center{
				position: relative;
				background-color: #FBFDFF;
				padding: 32rpx 38rpx;
				padding-top: 12rpx;
				border-bottom: 2rpx solid #E2E5ED;
				.item{
					display: flex;
					align-items: center;
					margin-top: 20rpx;
					
					.name{
						width: 150rpx;
					}
					.value{
						flex: 1;
					}
					.right{
						display: flex;
						align-items: center;
						justify-content: flex-end;
					}
					.unit{
						margin-left: 10rpx;
					}
				}
				.m40{
					justify-content: space-between;
					margin-top: 20rpx;
				}
				.checkbox{
					position: absolute;
					top: 26rpx;
					right: 26rpx;
				}
			}
			
			.cause{
				background-color: #FFFFFF;
				padding: 38rpx 33rpx;
				
				.button{
					width: 100%;
					height: 92rpx;
					line-height: 92rpx;
					text-align: center;
					background: #2F80ED;
					color: #FFFFFF;
					margin-top: 65rpx;
					margin-bottom: 20rpx;
				}
			}
			.bgc{
				margin-top: 30rpx;
			}
		}
	}
</style>
